package kandl.automation.steprunner.selenium;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.beans.factory.annotation.Value;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import kandl.automation.Browser;
import kandl.automation.testsuite.TestProfile;

public class RemoteWebDriverFactory implements WebDriverFactory {

    private URL url;

    @Value("${selenium.timeout.seconds}")
    private int timeout;

    public RemoteWebDriverFactory(URL url) {
        this.url = url;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    @Override
    public WebDriver getWebDriver(TestProfile profile) {

        Browser browser = profile.getBrowser();
        DesiredCapabilities desiredCapabilities = getCapabilities(browser);
        desiredCapabilities.setVersion(profile.getBrowserVersion());
        desiredCapabilities.setCapability("platform", profile.getPlatform());
        String sessionLabel = System.getProperty("user.name") + " " + profile.getId();
        String extraLabel= System.getProperty("session.label");
        if (StringUtils.isNotBlank(extraLabel)) {
        	sessionLabel += (" " + extraLabel);
        }
		desiredCapabilities.setCapability("name", sessionLabel);

        WebDriver webDriver = new RemoteWebDriver(url, desiredCapabilities);
        webDriver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
        webDriver = new Augmenter().augment(webDriver); // Hopefully add TakeScreenshot capabilities
        return webDriver;
    }

    private DesiredCapabilities getCapabilities(Browser browser) {
        String type = browser.getType();

        if (type.equals(Browser.FIREFOX_TYPE)) {
            return DesiredCapabilities.firefox();
        }
        else if (type.equals(Browser.CHROME_TYPE)) {
            return DesiredCapabilities.chrome();
        }
        else if (type.equals(Browser.IE_TYPE1)) {
            return DesiredCapabilities.internetExplorer();
        }
        else if (type.equals(Browser.IE_TYPE2)) {
            return DesiredCapabilities.internetExplorer();
        }
        else if (type.equals(Browser.IE_TYPE3)) {
            return DesiredCapabilities.internetExplorer();
        }
        else if (type.equals(Browser.IE_TYPE4)) {
            return DesiredCapabilities.internetExplorer();
        }
        else if (type.equals(Browser.SAFARI_TYPE)) {
            return DesiredCapabilities.safari();
        }
        else if (type.equals(Browser.IPHONE_TYPE)) {
            DesiredCapabilities capabilities = DesiredCapabilities.iphone();
            capabilities.setCapability("device-orientation", "portrait");
            return capabilities;
        }
        else if (type.equals(Browser.IPAD_TYPE)) {
            DesiredCapabilities capabilities = DesiredCapabilities.ipad();
            capabilities.setCapability("device-orientation", "portrait");
            return capabilities;
        }
        else if (type.equals(Browser.ANDROID_PHONE_TYPE)) {
            DesiredCapabilities capabilities = DesiredCapabilities.android();
            capabilities.setCapability("device-orientation", "portrait");
            return capabilities;
        }
        else if (type.equals(Browser.ANDROID_TABLET_TYPE)) {
            DesiredCapabilities capabilities = DesiredCapabilities.android();
            capabilities.setCapability("device-type", "tablet");
            capabilities.setCapability("device-orientation", "portrait");
            return capabilities;
        }

        throw new UnsupportedOperationException("Cannot start a Webdriver for " + browser);
    }
}
